﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using ECONTACT.Bussiness;
using ECONTACT.Controller;
using ECONTACT.DataLayer;

namespace ECONTACT
{
    public partial class frmMain : Office2007RibbonForm
    {
        frmConnection   m_Connection    = null;
        frmDangNhap m_FrmLogin = null;
        frmHocSinh m_FrmHocSinh = null;
        frmGiaoVien m_FrmGiaoVien = null;
        frmPhuHuynh m_FrmPhuHuynh = null;
        frmHocKy m_FrmHocKy = null;
        frmNamHoc m_frmNamHoc = null;
        frmMonHoc m_FrmMonHoc = null;
        frmKhoiLop m_FrmKhoiLop = null;
        frmLop m_FrmLop = null;
        frmTTTaiKhoan m_FrmTTTaiKhoan = null;
        frmNhapDiem m_FrmNhapDiem = null;
        frmXemDiem m_FrmXemDiem = null;
        frmDSHocSinhTheoLop m_FrmDSHocSinhTheoLop = null;
        frmHocLuc m_FrmHocLuc = null;
        frmHanhKiem m_FrmHanhKiem = null;
        frmPhanLop m_FrmPhanLop = null;

        TaiKhoanCtrl m_TaiKhoanCtrl = new TaiKhoanCtrl();

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            if (DataService.OpenConnection())
            {
                //MessageBoxEx.Show("Kết nối thành công 10 đ hí hí!", "SUCCESSED", MessageBoxButtons.OK, MessageBoxIcon.Information);
                GiaoDienDefault();
                DangNhap();
            }
            else
            {
                GiaoDienDefault();
                ReConnection();
            }
        }

        #region Kết nối lại CSDL khi chua chon
        public void ReConnection()
        {
            MessageBoxEx.Show("Lỗi kết nối đến cơ sở dữ liệu! Xin vui lòng thiết lập lại kết nối...", "ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);

            if (m_Connection == null || m_Connection.IsDisposed)
                //{
                m_Connection = new frmConnection();
            //}

            if (m_Connection.ShowDialog() == DialogResult.OK)
            {
                MessageBoxEx.Show("Đã thiết lập kết nối cho lần chạy đầu tiên.\nHãy khởi động lại chương trình để thực thi kết nối!", "SUCCESSED", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                return;
            }
        }
        #endregion

        private void buttonItemConnection_Click(object sender, EventArgs e)
        {
            if (m_Connection == null || m_Connection.IsDisposed)
                //{
                m_Connection = new frmConnection();
            //}

            if (m_Connection.ShowDialog() == DialogResult.OK)
            {
                MessageBoxEx.Show("Đã thiết lập kết nối cho lần chạy đầu tiên.\nHãy khởi động lại chương trình để thực thi kết nối!", "SUCCESSED", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                return;
            }
        }
        private void ButtonItemDangNhap_Click(object sender, EventArgs e)
        {
            if (m_FrmLogin == null || m_FrmLogin.IsDisposed)
            {
                m_FrmLogin = new frmDangNhap();
                //m_FrmLogin.MdiParent = this;
                //m_FrmLogin.Show();
                //m_FrmLogin.ShowDialog();
            }else{
                //m_FrmLogin.Activate();
                //m_FrmLogin.ShowDialog();
            }

            m_FrmLogin.txtUsername.Text = "";
            m_FrmLogin.txtPassword.Text = "";
            m_FrmLogin.lblUserError.Text = "";
            m_FrmLogin.lblPassError.Text = "";
            DangNhap();
        }

        private void ButtonItemThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonItemHocSinh_Click(object sender, EventArgs e)
        {
            if (m_FrmHocSinh == null || m_FrmHocSinh.IsDisposed)
            {
                m_FrmHocSinh = new frmHocSinh();
                m_FrmHocSinh.MdiParent = this;
                m_FrmHocSinh.MdiParent = frmMain.ActiveForm;
                m_FrmHocSinh.FormBorderStyle = FormBorderStyle.None;
                m_FrmHocSinh.BackColor = Color.AliceBlue;
                m_FrmHocSinh.Dock = DockStyle.Fill;
                m_FrmHocSinh.Show();

                this.Text = "ECONTACT - [H]ọc [S]inh";
            }
            else
                m_FrmHocSinh.Activate();
        }

        private void buttonItemGiaoVien_Click(object sender, EventArgs e)
        {
            if (m_FrmGiaoVien == null || m_FrmGiaoVien.IsDisposed)
            {
                m_FrmGiaoVien = new frmGiaoVien();
                m_FrmGiaoVien.MdiParent = this;
                m_FrmGiaoVien.MdiParent = frmMain.ActiveForm;
                m_FrmGiaoVien.FormBorderStyle = FormBorderStyle.None;
                m_FrmGiaoVien.BackColor = Color.AliceBlue;
                m_FrmGiaoVien.Dock = DockStyle.Fill;
                m_FrmGiaoVien.Show();

                this.Text = "ECONTACT - [G]iáo [V]iên";
            }
            else
                m_FrmGiaoVien.Activate();
        }

        private void buttonItemPhuHuynh_Click(object sender, EventArgs e)
        {
            if (m_FrmPhuHuynh == null || m_FrmPhuHuynh.IsDisposed)
            {
                m_FrmPhuHuynh = new frmPhuHuynh();
                m_FrmPhuHuynh.MdiParent = this;
                m_FrmPhuHuynh.MdiParent = frmMain.ActiveForm;
                m_FrmPhuHuynh.FormBorderStyle = FormBorderStyle.None;
                m_FrmPhuHuynh.BackColor = Color.AliceBlue;
                m_FrmPhuHuynh.Dock = DockStyle.Fill;
                m_FrmPhuHuynh.Show();

                this.Text = "ECONTACT - [P]hụ [H]uynh";
            }
            else
                m_FrmPhuHuynh.Activate();
        }

        private void buttonItemHocKy_Click(object sender, EventArgs e)
        {
            if (m_FrmHocKy == null || m_FrmHocKy.IsDisposed)
            {
                m_FrmHocKy = new frmHocKy();
                m_FrmHocKy.MdiParent = this;
                m_FrmHocKy.MdiParent = frmMain.ActiveForm;
                m_FrmHocKy.FormBorderStyle = FormBorderStyle.None;
                m_FrmHocKy.BackColor = Color.AliceBlue;
                m_FrmHocKy.Dock = DockStyle.Fill;
                m_FrmHocKy.Show();

                this.Text = "ECONTACT - [H]oc[K]y";
            }
            else
                m_FrmHocKy.Activate();
        }

        private void buttonItemNamHoc_Click(object sender, EventArgs e)
        {
            if (m_frmNamHoc == null || m_frmNamHoc.IsDisposed)
            {
                m_frmNamHoc = new frmNamHoc();
                m_frmNamHoc.MdiParent = this;
                m_frmNamHoc.MdiParent = frmMain.ActiveForm;
                m_frmNamHoc.FormBorderStyle = FormBorderStyle.None;
                m_frmNamHoc.BackColor = Color.AliceBlue;
                m_frmNamHoc.Dock = DockStyle.Fill;
                m_frmNamHoc.Show();

                this.Text = "ECONTACT - [H]oc[K]y";
            }
            else
                m_frmNamHoc.Activate();
        }

        private void buttonItemMonHoc_Click(object sender, EventArgs e)
        {
            if (m_FrmMonHoc == null || m_FrmMonHoc.IsDisposed)
            {
                m_FrmMonHoc = new frmMonHoc();
                m_FrmMonHoc.MdiParent = this;
                m_FrmMonHoc.MdiParent = frmMain.ActiveForm;
                m_FrmMonHoc.FormBorderStyle = FormBorderStyle.None;
                m_FrmMonHoc.BackColor = Color.AliceBlue;
                m_FrmMonHoc.Dock = DockStyle.Fill;
                m_FrmMonHoc.Show();

                this.Text = "ECONTACT - [M]on[H]oc";
            }
            else
                m_FrmMonHoc.Activate();
        }

        private void buttonItemKhoiLop_Click(object sender, EventArgs e)
        {
            if (m_FrmKhoiLop == null || m_FrmKhoiLop.IsDisposed)
            {
                m_FrmKhoiLop = new frmKhoiLop();
                m_FrmKhoiLop.MdiParent = this;
                m_FrmKhoiLop.MdiParent = frmMain.ActiveForm;
                m_FrmKhoiLop.FormBorderStyle = FormBorderStyle.None;
                m_FrmKhoiLop.BackColor = Color.AliceBlue;
                m_FrmKhoiLop.Dock = DockStyle.Fill;
                m_FrmKhoiLop.Show();

                this.Text = "ECONTACT - [K]hối [L]ớp";
            }
            else
                m_FrmKhoiLop.Activate();
        }

        private void buttonItemLop_Click(object sender, EventArgs e)
        {
            if (m_FrmLop == null || m_FrmLop.IsDisposed)
            {
                m_FrmLop = new frmLop();
                m_FrmLop.MdiParent = this;
                m_FrmLop.MdiParent = frmMain.ActiveForm;
                m_FrmLop.FormBorderStyle = FormBorderStyle.None;
                m_FrmLop.BackColor = Color.AliceBlue;
                m_FrmLop.Dock = DockStyle.Fill;
                m_FrmLop.Show();

                this.Text = "ECONTACT - [L]ớp";
            }
            else
                m_FrmLop.Activate();
        }

        private void buttonItemTTTaiKhoan_Click(object sender, EventArgs e)
        {
            if (m_FrmTTTaiKhoan == null || m_FrmTTTaiKhoan.IsDisposed)
            {
                m_FrmTTTaiKhoan = new frmTTTaiKhoan();
                m_FrmTTTaiKhoan.MdiParent = this;
                m_FrmTTTaiKhoan.MdiParent = frmMain.ActiveForm;
                m_FrmTTTaiKhoan.FormBorderStyle = FormBorderStyle.None;
                m_FrmTTTaiKhoan.BackColor = Color.AliceBlue;
                m_FrmTTTaiKhoan.Dock = DockStyle.Fill;
                m_FrmTTTaiKhoan.Show();

                this.Text = "ECONTACT - [T]hông [T]in";
            }
            else
                m_FrmTTTaiKhoan.Activate();
        }

        private void buttonItemNhapDiem_Click(object sender, EventArgs e)
        {
            if (m_FrmNhapDiem == null || m_FrmNhapDiem.IsDisposed)
            {
                m_FrmNhapDiem = new frmNhapDiem();
                m_FrmNhapDiem.MdiParent = this;
                m_FrmNhapDiem.MdiParent = frmMain.ActiveForm;
                m_FrmNhapDiem.FormBorderStyle = FormBorderStyle.None;
                m_FrmNhapDiem.BackColor = Color.AliceBlue;
                m_FrmNhapDiem.Dock = DockStyle.Fill;
                m_FrmNhapDiem.Show();

                this.Text = "ECONTACT - [N]hập [Đ]iểm";
            }
            else
                m_FrmNhapDiem.Activate();
        }

        private void buttonItemXemDiem_Click(object sender, EventArgs e)
        {
            if (m_FrmXemDiem == null || m_FrmXemDiem.IsDisposed)
            {
                m_FrmXemDiem = new frmXemDiem();
                m_FrmXemDiem.MdiParent = this;
                m_FrmXemDiem.MdiParent = frmMain.ActiveForm;
                m_FrmXemDiem.FormBorderStyle = FormBorderStyle.None;
                m_FrmXemDiem.BackColor = Color.AliceBlue;
                m_FrmXemDiem.Dock = DockStyle.Fill;
                m_FrmXemDiem.Show();

                this.Text = "ECONTACT - [X]em [Đ]iểm";
            }
            else
                m_FrmXemDiem.Activate();
        }

        private void buttonItemXemHSTheoLop_Click(object sender, EventArgs e)
        {
            if (m_FrmDSHocSinhTheoLop == null || m_FrmDSHocSinhTheoLop.IsDisposed)
            {
                m_FrmDSHocSinhTheoLop = new frmDSHocSinhTheoLop();
                m_FrmDSHocSinhTheoLop.MdiParent = this;
                m_FrmDSHocSinhTheoLop.MdiParent = frmMain.ActiveForm;
                m_FrmDSHocSinhTheoLop.FormBorderStyle = FormBorderStyle.None;
                m_FrmDSHocSinhTheoLop.BackColor = Color.AliceBlue;
                m_FrmDSHocSinhTheoLop.Dock = DockStyle.Fill;
                m_FrmDSHocSinhTheoLop.Show();

                this.Text = "ECONTACT - [D]anh [S]ách [H]ọc [S]inh [T]heo [L]ớp";
            }
            else
                m_FrmDSHocSinhTheoLop.Activate();
        }

        private void buttonItemHocLuc_Click(object sender, EventArgs e)
        {
            if (m_FrmHocLuc == null || m_FrmHocLuc.IsDisposed)
            {
                m_FrmHocLuc = new frmHocLuc();
                m_FrmHocLuc.MdiParent = this;
                m_FrmHocLuc.MdiParent = frmMain.ActiveForm;
                m_FrmHocLuc.FormBorderStyle = FormBorderStyle.None;
                m_FrmHocLuc.BackColor = Color.AliceBlue;
                m_FrmHocLuc.Dock = DockStyle.Fill;
                m_FrmHocLuc.Show();

                this.Text = "ECONTACT - [H]ọc [L]ực";
            }
            else
                m_FrmHocLuc.Activate();
        }

        private void buttonItemHanhKiem_Click(object sender, EventArgs e)
        {
            if (m_FrmHanhKiem == null || m_FrmHanhKiem.IsDisposed)
            {
                m_FrmHanhKiem = new frmHanhKiem();
                m_FrmHanhKiem.MdiParent = this;
                m_FrmHanhKiem.MdiParent = frmMain.ActiveForm;
                m_FrmHanhKiem.FormBorderStyle = FormBorderStyle.None;
                m_FrmHanhKiem.BackColor = Color.AliceBlue;
                m_FrmHanhKiem.Dock = DockStyle.Fill;
                m_FrmHanhKiem.Show();

                this.Text = "ECONTACT - [H]ạnh [K]iểm";
            }
            else
                m_FrmHanhKiem.Activate();
        }

        private void buttonItemPhanLop_Click(object sender, EventArgs e)
        {
            if (m_FrmPhanLop == null || m_FrmPhanLop.IsDisposed)
            {
                m_FrmPhanLop = new frmPhanLop();
                m_FrmPhanLop.MdiParent = this;
                m_FrmPhanLop.MdiParent = frmMain.ActiveForm;
                m_FrmPhanLop.FormBorderStyle = FormBorderStyle.None;
                m_FrmPhanLop.BackColor = Color.AliceBlue;
                m_FrmPhanLop.Dock = DockStyle.Fill;
                m_FrmPhanLop.Show();

                this.Text = "ECONTACT - [P]hân [L]ớp";
            }
            else
                m_FrmPhanLop.Activate();
        }

        #region DangNhap
        public void DangNhap()
        {
        Cont:
            if (m_FrmLogin == null || m_FrmLogin.IsDisposed)
                m_FrmLogin = new frmDangNhap();

            if (m_FrmLogin.ShowDialog() == DialogResult.OK)
            {
                if (m_FrmLogin.txtUsername.Text == "")
                {
                    m_FrmLogin.lblPassError.Text = "";
                    m_FrmLogin.lblUserError.Text = "Bạn chưa nhập tên!";
                    goto Cont;
                }

                if (m_FrmLogin.txtPassword.Text == "")
                {
                    m_FrmLogin.lblUserError.Text = "";
                    m_FrmLogin.lblPassError.Text = "Bạn chưa nhập mật khẩu!";
                    goto Cont;
                }

                int ketQua = m_TaiKhoanCtrl.DangNhap(m_FrmLogin.txtUsername.Text, m_FrmLogin.txtPassword.Text);

                switch (ketQua)
                {
                    case 0:
                        m_FrmLogin.lblPassError.Text = "";
                        m_FrmLogin.lblUserError.Text = "Người dùng này không tồn tại!";
                        goto Cont;
                    case 1:
                        m_FrmLogin.lblUserError.Text = "";
                        m_FrmLogin.lblPassError.Text = "Mật khẩu không hợp lệ!";
                        goto Cont;
                    case 2:
                        lblTenNguoiDung.Text = Utilities.NguoiDung.HoTen;
                        //lblTenNguoiDung.Text = "as";
                        PhanQuyen(Utilities.NguoiDung.MaLoaiTK);
                        //GiaoDienQT();
                        break;
                }
            }
            else
                return;
        }
        #endregion

        public void PhanQuyen(String MaLTT)
        {
            switch (MaLTT)
            {
                case "1": GiaoDienQT(); 
                    break;
                case "2": GiaoDienGV();
                    break;
                case "3": GiaoDienPH();
                    break;
                case "4": GiaoDienHS();
                    break;
            }
        }

        private void ButtonItemDangXuat_Click(object sender, EventArgs e)
        {
            lblTenNguoiDung.Text = "Chưa đăng nhập !";
            GiaoDienDefault();
        }

        public void GiaoDienDefault()
        {
            ButtonItemDangNhap.Enabled = true;
            ButtonItemThoat.Enabled = true;
            //---
            ButtonItemDangXuat.Enabled = false;
            buttonItemHocSinh.Enabled = false;
            buttonItemGiaoVien.Enabled = false;
            buttonItemPhuHuynh.Enabled = false;
            buttonItemHocKy.Enabled = false;
            buttonItemNamHoc.Enabled = false;
            buttonItemMonHoc.Enabled = false;
            buttonItemNhapDiem.Enabled = false;
            buttonItemXemDiem.Enabled = false;
            buttonItemKhoiLop.Enabled = false;
            buttonItemLop.Enabled = false;
            buttonItemTTTaiKhoan.Enabled = false;
            buttonItemXemHSTheoLop.Enabled = false;
            buttonItemHocLuc.Enabled = false;
            buttonItemHanhKiem.Enabled = false;
            buttonItemPhanLop.Enabled = false;
        }

        public void GiaoDienQT()
        {
            ButtonItemDangNhap.Enabled = true;
            ButtonItemThoat.Enabled = true;
            //---
            ButtonItemDangXuat.Enabled = true;
            buttonItemHocSinh.Enabled = true;
            buttonItemGiaoVien.Enabled = true;
            buttonItemPhuHuynh.Enabled = true;
            buttonItemHocKy.Enabled = true;
            buttonItemNamHoc.Enabled = true;
            buttonItemMonHoc.Enabled = true;
            buttonItemNhapDiem.Enabled = true;
            buttonItemXemDiem.Enabled = true;
            buttonItemKhoiLop.Enabled = true;
            buttonItemLop.Enabled = true;
            buttonItemTTTaiKhoan.Enabled = true;
            buttonItemXemHSTheoLop.Enabled = true;
            buttonItemHocLuc.Enabled = true;
            buttonItemHanhKiem.Enabled = true;
            buttonItemPhanLop.Enabled = true;
        }

        public void GiaoDienGV()
        {
            ButtonItemDangNhap.Enabled = true;
            ButtonItemThoat.Enabled = true;
            //---
            ButtonItemDangXuat.Enabled = true;
            buttonItemHocSinh.Enabled = true;
            buttonItemGiaoVien.Enabled = false;
            buttonItemPhuHuynh.Enabled = false;
            buttonItemHocKy.Enabled = false;
            buttonItemNamHoc.Enabled = false;
            buttonItemMonHoc.Enabled = true;
            buttonItemNhapDiem.Enabled = true;
            buttonItemXemDiem.Enabled = true;
            buttonItemKhoiLop.Enabled = false;
            buttonItemLop.Enabled = false;
            buttonItemTTTaiKhoan.Enabled = true;
            buttonItemXemHSTheoLop.Enabled = true;
            buttonItemHocLuc.Enabled = true;
            buttonItemHanhKiem.Enabled = true;
            buttonItemPhanLop.Enabled = false;
        }

        // se bo sung phan tong ket sau cho PH
        public void GiaoDienPH()
        {
            ButtonItemDangNhap.Enabled = true;
            ButtonItemThoat.Enabled = true;
            //---
            ButtonItemDangXuat.Enabled = true;
            buttonItemHocSinh.Enabled = false;
            buttonItemGiaoVien.Enabled = false;
            buttonItemPhuHuynh.Enabled = false;
            buttonItemHocKy.Enabled = false;
            buttonItemNamHoc.Enabled = false;
            buttonItemMonHoc.Enabled = false;
            buttonItemNhapDiem.Enabled = false;
            buttonItemXemDiem.Enabled = true;
            buttonItemKhoiLop.Enabled = false;
            buttonItemLop.Enabled = false;
            buttonItemTTTaiKhoan.Enabled = true;
            buttonItemXemHSTheoLop.Enabled = true;
            buttonItemHocLuc.Enabled = false;
            buttonItemHanhKiem.Enabled = false;
            buttonItemPhanLop.Enabled = false;
        }

        public void GiaoDienHS()
        {
            ButtonItemDangNhap.Enabled = true;
            ButtonItemThoat.Enabled = true;
            //---
            ButtonItemDangXuat.Enabled = true;
            buttonItemHocSinh.Enabled = false;
            buttonItemGiaoVien.Enabled = false;
            buttonItemPhuHuynh.Enabled = false;
            buttonItemHocKy.Enabled = false;
            buttonItemNamHoc.Enabled = false;
            buttonItemMonHoc.Enabled = false;
            buttonItemNhapDiem.Enabled = false;
            buttonItemXemDiem.Enabled = true;
            buttonItemKhoiLop.Enabled = false;
            buttonItemLop.Enabled = false;
            buttonItemTTTaiKhoan.Enabled = false;
            buttonItemXemHSTheoLop.Enabled = false;
            buttonItemHocLuc.Enabled = false;
            buttonItemHanhKiem.Enabled = false;
            buttonItemPhanLop.Enabled = false;
        }

    }
}
